package com.jf.mall.dao.impl;

import com.jf.mall.dao.BasicDAO;
import com.jf.mall.dao.CommodityDAO;
import com.jf.mall.domain.Commodity;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-10-08
 * Time:18:05
 */
public class CommodityImpl extends BasicDAO<Commodity> implements CommodityDAO {
    @Override
    public List<Commodity> getCommoditiesByUserId(int userId) {
        String sql = "select id, user_id userId, furniture_id furnitureId, `name`, unit_price unitPrice, `count`, total_price totalPrice, img_path imgPath, update_time updateTime from commodity where user_id=? order by update_time desc";
        return queryMulti(sql, Commodity.class, userId);
    }

    @Override
    public HashSet<Integer> getCommFurnIdsByUserId(int userId) {
        String sql = "select furniture_id from commodity where user_id=?";
        List<Integer> ids = querySingleList(sql, Integer.class, userId);
        HashSet<Integer> commFurnIds = new HashSet<>();
        commFurnIds.addAll(ids);
        return commFurnIds;
    }

    @Override
    public int addCommodity(Commodity commodity) {
        String sql = "insert into commodity(user_id, furniture_id, `name`, unit_price, `count`, total_price, img_path, update_time) values(?,?,?,?,?,?,?,now())";
        Integer userId = commodity.getUserId();
        Integer furnitureId = commodity.getFurnitureId();
        String name = commodity.getName();
        BigDecimal unitPrice = commodity.getUnitPrice();
        Integer count = commodity.getCount();
        BigDecimal totalPrice = commodity.getTotalPrice();
        String imgPath = commodity.getImgPath();
        try {
            return update(sql, userId, furnitureId, name, unitPrice, count, totalPrice, imgPath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updateCommodity(Commodity commodity) {
        String sql = "update commodity set `name`=?, unit_price=?, `count`=?, total_price=?, img_path=?, update_time=now() where user_id=? and furniture_id=?";
        Integer userId = commodity.getUserId();
        Integer furnitureId = commodity.getFurnitureId();
        String name = commodity.getName();
        BigDecimal unitPrice = commodity.getUnitPrice();
        Integer count = commodity.getCount();
        BigDecimal totalPrice = commodity.getTotalPrice();
        String imgPath = commodity.getImgPath();
        try {
            return update(sql, name, unitPrice, count, totalPrice, imgPath, userId, furnitureId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteCommodity(int commodityId) {
        String sql = "delete from commodity where id=?";
        try {
            return update(sql, commodityId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteUserCommodities(int userId) {
        String sql = "delete from commodity where user_id=?";
        try {
            return update(sql, userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
